import 'package:bruno/bruno.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import '../../view/RadiusBorder.dart';
import '../rscamera_page/view.dart';
import 'logic.dart';

class HomePage extends StatelessWidget {
  final logic = Get.put(HomeLogic());
  final state = Get.find<HomeLogic>().state;

  HomePage({super.key});

  @override
  Widget build(BuildContext context) {
    return ConstrainedBox(
        constraints: const BoxConstraints.expand(),
        child: Stack(
          alignment: Alignment.topCenter,
          fit: StackFit.loose,
          children: [
            Container(color: Colors.white, height: 380.h),
            Image.asset(state.homeBg,
                fit: BoxFit.fill, width: double.infinity, height: 180.h),
            Column(
              children: [buildSearch(context), buildRefresh(context)],
            )
          ],
        ));
  }

  ///构建首页搜索栏
  Widget buildSearch(BuildContext context) {
    return Padding(
        padding: EdgeInsets.only(bottom: 10.h),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            RadiusSearchBorder(
              radius: 20.w,
              borderColor: Colors.white,
              searchWidth: 0.75.sw,
              searchHeight: 35.h,
              onTap: () =>
                  BrnToast.showInCenter(text: "跳转到搜索页面", context: context),
            ),
            Padding(
                padding: EdgeInsets.only(
                    top: ScreenUtil().statusBarHeight + 10.h, right: 20.w),
                child: InkWell(
                    child: Image.asset(state.scanIcon, height: 22),
                    onTap: () => BrnToast.showInCenter(
                        text: "跳转到扫码页面", context: context)))
          ],
        ));
  }

  ///构建下拉刷新控件间
  Widget buildRefresh(BuildContext context) {
    return Expanded(
        child: EasyRefresh(
            controller: state.refreshController,
            child: CustomScrollView(slivers: [
              SliverPadding(
                  padding: EdgeInsets.only(left: 15.w, top: 30.h, right: 15.w),
                  sliver: SliverToBoxAdapter(
                      child: BrnShadowCard(
                          color: Colors.white,
                          circular: 10,
                          child: GridView.count(
                            physics: const NeverScrollableScrollPhysics(),
                            padding: EdgeInsets.only(top: 10.h, bottom: 10.h),
                            shrinkWrap: true,
                            crossAxisCount: 4,
                            childAspectRatio: 1.5,
                            mainAxisSpacing: 20,
                            children: buildMenu(),
                          )))),
              SliverPadding(
                  padding: EdgeInsets.only(top: 20.h, left: 10.w),
                  sliver: SliverGrid.count(
                      crossAxisCount: 4, children: buildCenterMenu())),
              SliverPadding(
                  padding: EdgeInsets.only(top: 20.h),
                  sliver:
                      SliverToBoxAdapter(child: buildTodayJourney(context))),
              SliverPadding(
                  padding: EdgeInsets.only(top: 10.h),
                  sliver: SliverToBoxAdapter(child: buildTreasureBox()))
            ]),
            onRefresh: () async {
              await Future.delayed(const Duration(seconds: 2), () {
                state.refreshController.finishRefresh();
              });
            }));
  }

  ///构建首页顶部菜单
  List<Widget> buildMenu() {
    var widgets = <Widget>[];
    for (int i = 0; i < state.mainMenuIconList.length; i++) {
      widgets.add(Column(children: [
        Image.asset(state.mainMenuIconList[i], width: 35.w, height: 35.h),
        Text(state.mainMenuTextList[i],
            style: const TextStyle(color: Colors.blueGrey))
      ]));
    }
    return widgets;
  }

  ///构建首页中部菜单
  List<Widget> buildCenterMenu() {
    var widgets = <Widget>[];
    for (int i = 0; i < state.centerMenuIconList.length; i++) {
      widgets.add(Column(children: [
        Image.asset(state.centerMenuIconList[i], width: 50.w, height: 50.h),
        Padding(
            padding: EdgeInsets.only(top: 8.h),
            child: Text(state.centerMenuTextList[i],
                style: const TextStyle(color: Colors.black)))
      ]));
    }
    return widgets;
  }

  ///今日行程模块
  Widget buildTodayJourney(BuildContext context) {
    return Column(children: [
      Row(children: [
        Padding(
            padding: EdgeInsets.only(left: 15.w),
            child: Text("今日行程",
                style: TextStyle(color: Colors.black, fontSize: 18.sp))),
        Expanded(
            child: InkWell(
                child: Container(
                  alignment: Alignment.centerLeft,
                  padding: EdgeInsets.only(left: 10.w),
                  child: Image.asset(state.indexTipIcon, width: 18.w),
                ),
                onTap: () => {
                      showDialog(
                          context: context,
                          builder: (_) => BrnScrollableTextDialog(
                              title: "行程量说明", contentText:state.todayTips ,submitText: "我知道了",submitBgColor: Colors.green, onSubmitClick: (){
                                Navigator.of(context).pop();
                          }))
                    })),
        Padding(
            padding: EdgeInsets.only(right: 20.w),
            child: BrnTabBar(
              tabs: state.todayTabList,
              controller: logic.tabController,
              backgroundcolor: Colors.transparent,
              tabWidth: 40.w,
              labelColor: Colors.green,
              unselectedLabelColor: Colors.grey,
              indicatorColor: Colors.green,
              indicatorWidth: 15.w,
              indicatorWeight: 3.h,
              isScroll: false,
              onTap: (state, index) => {logic.changTab(index)},
            ))
      ]),
      SizedBox(
          height: 200.h,
          child: PageView.builder(
              controller: logic.pageController,
              onPageChanged: logic.changPage,
              itemCount: state.todayTabList.length,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return Padding(
                    padding: EdgeInsets.only(
                        top: 15.h, left: 15.w, right: 15.w, bottom: 15.h),
                    child: BrnShadowCard(
                        color: Colors.white,
                        circular: 8,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            GridView.count(
                                physics: const NeverScrollableScrollPhysics(),
                                padding: EdgeInsets.only(top: 15.h),
                                shrinkWrap: true,
                                crossAxisCount: 4,
                                childAspectRatio: 2,
                                mainAxisSpacing: 20,
                                children: buildTodayTextList()),
                            const Padding(
                                padding: EdgeInsets.only(left: 20, top: 10),
                                child: Text(
                                    "* 数据更新于：2023-4-16 16:23 最终数据以报表中心为准",
                                    style: TextStyle(
                                        fontSize: 11,
                                        color: Colors.indigoAccent)))
                          ],
                        )));
              }))
    ]);
  }

  List<Widget> buildTodayTextList() {
    var widgets = <Widget>[];
    for (int i = 0; i < state.todayKeyList.length; i++) {
      widgets.add(Column(children: [
        Text(state.todayValueList[i],
            style: TextStyle(fontSize: 16.sp, color: Colors.indigo)),
        Text(state.todayKeyList[i],
            style: TextStyle(fontSize: 12.sp, color: Colors.indigoAccent))
      ]));
    }
    return widgets;
  }

  Widget buildTreasureBox() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Padding(
          padding: EdgeInsets.only(left: 15.w),
          child: Text("百宝箱",
              style: TextStyle(color: Colors.black, fontSize: 18.sp))),
      SizedBox(
          height: 210.h,
          child: PageView.builder(
              controller: logic.pageController,
              onPageChanged: logic.changPage,
              itemCount: state.todayTabList.length,
              physics: const NeverScrollableScrollPhysics(),
              itemBuilder: (context, index) {
                return GestureDetector(onTap:()=>{
                  Get.to(const RscameraPage())
                } ,child:  Padding(
                    padding: EdgeInsets.only(
                        top: 15.h, left: 15.w, right: 15.w, bottom: 15.h),
                    child: BrnShadowCard(
                        color: Colors.white,
                        circular: 8,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            GridView.count(
                              physics: const NeverScrollableScrollPhysics(),
                              shrinkWrap: true,
                              padding: EdgeInsets.only(top: 15.h),
                              crossAxisCount: 3,
                              childAspectRatio: 1.55,
                              children: buildTreasureBoxList(),
                            )
                          ],
                        ))));
              }
              ))
    ]);
  }

  List<Widget> buildTreasureBoxList() {
    var widgets = <Widget>[];
    for (int i = 0; i < state.treasureIconList.length; i++) {
      widgets.add(Column(children: [
        Image.asset(state.treasureIconList[i], width: 45.w, height: 45.h),
        Text(state.treasureTextList[i],
            style: const TextStyle(color: Colors.black))
      ]));
    }
    return widgets;
  }
}
